package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.lagou.base.BaseServlet;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.service.impl.CourseContentServiceImpl;
import com.lagou.utils.DateUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/courseContent")
public class CourseContentServlet extends BaseServlet {

    public void findSectionAndLessonByCourseId(HttpServletRequest request, HttpServletResponse response) {

        try {
            String courseId = request.getParameter("course_id");

            CourseContentService service = new CourseContentServiceImpl();
            List<Course_Section> section = service.findSectionAndLessonByCourseId(Integer.parseInt(courseId));

            String result = JSON.toJSONString(section);
            response.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void findCourseByCourseId(HttpServletRequest request, HttpServletResponse response) {

        try {
            String courseId = request.getParameter("course_id");

            CourseContentService service = new CourseContentServiceImpl();
            Course course = service.findCourseByCourseId(Integer.parseInt(courseId));

            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class, "id", "course_name");

            String result = JSON.toJSONString(course, filter);

            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void saveOrUpdateSection(HttpServletRequest request ,HttpServletResponse response){

        try {
            //1.获取参数  从域对象中获取
            Map<String,Object> map = (Map)request.getAttribute("map");

            //2.创建Course_Section
            Course_Section section = new Course_Section();

            //3.使用BeanUtils工具类,将map中的数据封装到 section
            //BeanUtils.populate(section,map);

            //3.使用BeanUtils的 copyProperties方法,将map中的数据封装到section对象里
            BeanUtils.copyProperties(section,map.get("section"));

            //4.业务处理
            CourseContentService contentService = new CourseContentServiceImpl();

            //判断是否携带id
            if(section.getId() == 0){
                //新增操作
                String result = contentService.saveSection(section);
                //5.响应结果
                response.getWriter().print(result);

            }else{
                //修改操作
                String result = contentService.updateSection(section);
                response.getWriter().print(result);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void updateSectionStatus(HttpServletRequest request, HttpServletResponse response) {

        try {
            String id = request.getParameter("id");
            String status = request.getParameter("status");

            CourseContentService service = new CourseContentServiceImpl();
            String result = service.updateSectionStatus(Integer.parseInt(id), Integer.parseInt(status));

            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void saveOrUpdateLesson(HttpServletRequest request, HttpServletResponse response) {

        try {
            Map<String, Object> map = (Map) request.getAttribute("map");
            System.out.println("lessonMap = " + map);

            Course_Lesson lesson = new Course_Lesson();
//            BeanUtils.populate(lesson, map);
            BeanUtils.copyProperties(lesson, map.get("lesson"));
            lesson.setUpdate_time(DateUtils.getDateFormat());

            CourseContentService service = new CourseContentServiceImpl();
            String result = null;
            if (0 == lesson.getId()) {
                //新增
                lesson.setCreate_time(DateUtils.getDateFormat());
                lesson.setStatus(1);
                result = service.saveLesson(lesson);
            } else {
                //修改
                result = service.updateLesson(lesson);
            }

            response.getWriter().print(result);
        } catch (Exception e) {
            System.out.println(12123);
            e.printStackTrace();
        }
    }
}
